
package rs.fon.rapidminer.operator.learner.tree;

import rs.fon.rapidminer.process.annotation.Parameter;
import com.rapidminer.example.ExampleSet;
import com.rapidminer.example.Attribute;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
import com.rapidminer.example.Attribute;
import com.rapidminer.example.Example;
import com.rapidminer.example.set.Partition;
import rs.fon.rapidminer.operator.learner.tree.SplittedExampleSet;

import rs.fon.rapidminer.process.annotation.Parameter; 

public class AllCategoricalPossibleSplit extends AbstractPossibleSplit {
	
	
	

//	public Object DoWork(ExampleSet exampleSet, List<Parameter> parameters)
//	{		
//		List<SplittedExampleSet> sviSplitedi = new LinkedList<SplittedExampleSet>();
//		
//		for(Attribute attribute : exampleSet.getAttributes())
//		{
//			ExampleSet trainingSet = (ExampleSet)exampleSet.clone();
//			SplittedExampleSet ses = this.Split(trainingSet, attribute);
//			sviSplitedi.add(ses);
//		}
//		
//		return sviSplitedi;
//	}
	
	public SplittedExampleSet Split(ExampleSet exampleSet, Attribute attribute)
	{
		int[] elements = new int[exampleSet.size()];
       
       
        LinkedList<String> categories =getAllCategories(exampleSet, attribute);
		int numOfSplits = categories.size();
		
		Iterator<Example> reader = exampleSet.iterator();
        int i = 0;
       
        while (reader.hasNext()) {
            Example example = reader.next();
            String currentValue = example.getValueAsString(attribute);
           
            for(int k = 0; k<categories.size();k++)
            {
            if (currentValue.equals(categories.get(k).toString()))
                elements[i] = k;
            
            }
            
            i++;
           
        }
        Partition partition = new Partition(elements, numOfSplits);
        
        SplittedExampleSet splitted =new SplittedExampleSet(exampleSet, partition);
        splitted.setAttribute(attribute);
        splitted.getAttributes().remove(attribute);

		
		return splitted;
	}

	@Override
	public Object DoWork(ExampleSet exampleSet, List<rs.fon.rapidminer.process.Parameter> parameters, boolean [] attributesForSplitting, double insignificanceParameter, int depth)
	    {
		// TODO Auto-generated method stub
           List<SplittedExampleSet> sviSplitedi = new LinkedList<SplittedExampleSet>();
		
       
           
           int i=0;
          
           
		for(Attribute attribute : exampleSet.getAttributes())
		{
			if (attribute.isNominal()) {
				
				if(attributesForSplitting[i])
        		{
			      ExampleSet trainingSet = (ExampleSet)exampleSet.clone();
			      SplittedExampleSet ses = this.Split(trainingSet, attribute);
			       sviSplitedi.add(ses);
        		}
			}
			i++;
		}
		
		return sviSplitedi;
	}

// Prebaciti u nadredjenu klasu
	 
	 public  LinkedList<String> getAllCategories(ExampleSet exampleSet, Attribute attribute) {
	        LinkedList<String> allCategoryList  =  new LinkedList<String>();
	   
	       
	        Iterator<Example> reader = exampleSet.iterator();
	       
	        while (reader.hasNext()) {
	            Example example = reader.next();
	            String currentValue = example.getValueAsString(attribute);
	            if (!inList(currentValue,allCategoryList))
	                allCategoryList.add(currentValue);
	        }
	       
	        //return new SplittedExampleSet(exampleSet, partition);
	        return allCategoryList;
	    }
	   
	    public  boolean inList(String category,List<String> allCategoryList) {
	       
	        for(int i =0; i<allCategoryList.size();i++)
	        {
	            if (allCategoryList.get(i).equals(category))
	                return true;
	         
	        }
	       
	       return false;
	    }
	
	
}
